﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace INSAForManagers.Views
{
    internal partial class FormEvalueList : Form
    {
        //부모 폼
        private FormACard formParent;

        //대상자 유형
        private int targetType;

        //DataGridView의 소스 DataTable
        private DataTable tblEvalueList = new DataTable();

        //DataGridViewRow
        private DataRow row;

        //개인번호
        private string pcode;

        //개인의 근무성적평정점/합산점
        private Hashtable personalEvalue;

        internal FormEvalueList(FormACard parent, int targetType)
        {
            this.formParent = parent;
            this.targetType = targetType;            

            this.pcode = parent.txtPCode.Text;



            InitializeComponent();
        }

        private void FormEvalueList_Load(object sender, EventArgs e)
        {
            try
            {
                //개인번호 확인
                if (this.pcode.Length != 10)
                {
                    MessageBox.Show("대상자를 먼저 선택하셔야 합니다.","확인", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }

                //Column 생성
                makeColumns();

                //Row 생성
                makeRows(targetType, (string)cboRefDate.SelectedItem);

                //교사
                if (targetType == 0)
                {
                    lblTargetType.Text = "교사";
                }
                //교감
                else
                {
                    lblTargetType.Text = "교감";
                }

                personalEvalue = getPersonalEvalue(pcode);

                if (personalEvalue != null)
                {
                    cboRefDate.SelectedItem = personalEvalue["REF_DATE"];
                    lblResult.Text = calcResult().ToString("#.000");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //Column 생성
        private void makeColumns()
        {
            DataColumn col = new DataColumn("연번");
            col.AllowDBNull = true;
            col.ReadOnly = false;
            col.DataType = typeof(string);
            tblEvalueList.Columns.Add(col);

            col = new DataColumn("기간");
            col.AllowDBNull = true;
            col.ReadOnly = false;
            col.DataType = typeof(string);
            tblEvalueList.Columns.Add(col);

            col = new DataColumn("비율(%)");
            col.AllowDBNull = true;
            col.ReadOnly = false;
            col.DataType = typeof(int);
            tblEvalueList.Columns.Add(col);

            col = new DataColumn("근무성적/합산점");
            col.AllowDBNull = true;
            col.ReadOnly = false;
            col.DataType = typeof(double);
            tblEvalueList.Columns.Add(col);

            dgvEvalueList.DataSource = tblEvalueList;

            dgvEvalueList.Columns[0].Width = 40;
            dgvEvalueList.Columns[0].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
            dgvEvalueList.Columns[1].Width = 100;
            dgvEvalueList.Columns[1].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
            dgvEvalueList.Columns[2].Width = 80;
            dgvEvalueList.Columns[2].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
            dgvEvalueList.Columns[3].Width = dgvEvalueList.Width - 236;


        }

        private void makeReadOnly(bool b)
        {
            dgvEvalueList.Columns[0].ReadOnly = b;
            dgvEvalueList.Columns[1].ReadOnly = b;
            dgvEvalueList.Columns[2].ReadOnly = b;
            if (b == true)
            {
                dgvEvalueList.EditMode = DataGridViewEditMode.EditOnEnter;
            }
        }

        private Hashtable getEvalueList(int targetType, string refDate)
        {
            Hashtable evalueList = DBUtil.getEvalueList(targetType, refDate);

            if (evalueList.Count > 0) return evalueList;

            return null;
        }

        private Hashtable getPersonalEvalue(string pcode)
        {
            Hashtable personalEvalue = DBUtil.getPersonalEvalue(pcode);

            if (personalEvalue.Count > 0) return personalEvalue;

            return null;
        }

        private void makeRows(int targetType, string refDate)
        {            

            Hashtable evalueList = getEvalueList(targetType, refDate);

            if (evalueList == null) return;

            int cnt = int.Parse(evalueList["CNT_YEAR"].ToString());

            if (dgvEvalueList.Rows.Count > 0)
            {
                makeReadOnly(false);
                tblEvalueList.Rows.Clear();
            }

            for (int i = 0; i < cnt; i++)
            {
                row = tblEvalueList.NewRow();
                row[0] = (i + 1).ToString();
                row[1] = "최근 " + (i + 1).ToString() + "년 이내";
                row[2] = int.Parse(evalueList["RATIO" + (i + 1).ToString()].ToString());
                if (personalEvalue != null && personalEvalue["POINT" + (i + 1).ToString()] != null) row[3] = personalEvalue["POINT" + (i + 1).ToString()];
                tblEvalueList.Rows.Add(row);
            }

            dgvEvalueList.DataSource = tblEvalueList;

            if (dgvEvalueList.Rows.Count > 0)
            {
                makeReadOnly(true);
            }


        }

        private void cboRefDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                makeRows(targetType, (string)cboRefDate.SelectedItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {

            lblResult.Text = calcResult().ToString("#.000");

        }

        
        private double calcResult()
        {
            if (cboRefDate.SelectedIndex == -1)
            {
                MessageBox.Show("기준일자를 먼저 선택하세요.   ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return 0.0;
            }

            int cnt = dgvEvalueList.Rows.Count;
            double result = 0.0;

            for (int i = 0; i < cnt; i++)
            {
                int ratio = Convert.ToInt32(dgvEvalueList.Rows[i].Cells[2].Value);
                double dRow = Convert.ToDouble(dgvEvalueList.Rows[i].Cells[3].Value);

                result += dRow * (double)((double)ratio / (double)100);
                                
            }

            return result;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveEvalueInfo())
                {
                    formParent.txtTotalEvaluePoint.Text = lblResult.Text;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool saveEvalueInfo()
        {
            double result = Convert.ToDouble(lblResult.Text);
            if (result != 0.0)
            {
                Hashtable hash = new Hashtable();
                hash["PCODE"] = pcode;
                hash["REF_DATE"] = cboRefDate.SelectedItem;
                hash["POINT_CNT"] = dgvEvalueList.Rows.Count;
                //MessageBox.Show(dgvEvalueList.Rows.Count.ToString());
                for (int i = 0; i < dgvEvalueList.Rows.Count; i++)
                {
                    hash["POINT" + (i + 1).ToString()] = dgvEvalueList.Rows[i].Cells[3].Value;
                }
                for (int i = dgvEvalueList.Rows.Count + 1; i <= 10; i++)
                {
                    hash["POINT" + i.ToString()] = 0.0;
                }
                hash["TOTAL_POINT"] = Convert.ToDouble(lblResult.Text);

                if (DBUtil.saveEvalueInfo(hash))
                {                    
                    return true;
                }
            }

            return false;
        }
    }
}
